<template>
  <el-dialog
    :destroy-on-close="true"
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="任务标题" prop="title">
        <el-input v-model="form.title" placeholder="请输入计划任务标题" />
      </el-form-item>
      <el-form-item label="任务接口" prop="task_service">
        <el-input
          v-model="form.task_service"
          placeholder="请输入计划任务接口"
        />
      </el-form-item>
      <el-form-item label="接口参数" prop="api_params">
        <el-input v-model="form.api_params" placeholder="请输入接口参数" />
      </el-form-item>
      <el-form-item label="运行间隔" prop="crontab_interval_time">
        <el-input-number
          v-model="form.crontab_interval_time"
          :min="10"
          :step="1"
          step-strictly
          @change="numberChange"
        />
      </el-form-item>

      <el-form-item label="启动状态" prop="enable">
        <el-switch
          v-model="form.enable"
          active-text="启动"
          :active-value="1"
          inactive-text="禁止"
          :inactive-value="0"
        />
      </el-form-item>

      <el-form-item label="进程状态" prop="state">
        <el-select v-model="form.state" placeholder="请选择">
          <el-option key="1" label="运行中" :value="1" />
          <el-option key="0" label="空闲" :value="0" />
          <el-option key="-1" label="异常退出" :value="-1" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { addTask, editTask } from '@/api/task'

  export default {
    name: 'TaskDetail',
    data() {
      return {
        form: {
          crontab_interval_time: 60,
          enable: 1,
          api_params: '',
        },
        rules: {
          title: [
            { required: true, trigger: 'blur', message: '请输入计划任务标题' },
          ],
          task_service: [
            { required: true, trigger: 'blur', message: '请输入计划任务接口' },
          ],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    methods: {
      numberChange(value) {
        this.form.crontab_interval_time = value
        this.$forceUpdate()
      },
      showEdit(row) {
        console.log(row)
        if (!row) {
          this.title = '添加'
        } else {
          this.title = '编辑'
          this.form = Object.assign({}, row)
          this.form.task_service = this.form.fire_params
        }
        this.dialogFormVisible = true
        this.$forceUpdate()
      },

      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            if (this.title == '添加') {
              const { data } = await addTask(this.form)
              if (data.is_success) {
                this.$baseMessage(
                  '添加成功',
                  'success',
                  'vab-hey-message-success'
                )
              }
            } else if (this.title == '编辑') {
              const { data } = await editTask(this.form)
              if (data.is_success) {
                this.$baseMessage(
                  '修改成功',
                  'success',
                  'vab-hey-message-success'
                )
              }
            }
            this.$emit('fetch-data')
            this.close()
          }
        })
      },
    },
  }
</script>

<style lang="scss" scoped>
  .vab-tree-border {
    height: 200px;
    padding: $base-padding;
    overflow-y: auto;
    border: 1px solid #dcdfe6;
    border-radius: $base-border-radius;
  }
</style>
